package ui;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;

import fast.MasterProcessor;

/**
 * Class for a textual user interface for a master
 * 
 * @author Adam Bell
 *
 */
public class TextUI {

	/**
	 * Main method loop containing decision logic based on user input
	 * 
	 * @param args
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void main(String[] args) throws InterruptedException, URISyntaxException, FileNotFoundException, IOException{
		
		//setup objects
		UserInput ui = new UserInput();
		MasterProcessor mp = MasterProcessor.getMasterProcessor();
		
		//setup vars
		boolean running = true;
		boolean command = false;
		String commandString = "";
		String dataString = "";
		
		
		//kickoff threads
		mp.start();		
		
		while(running){
			
			commandString = "";
			dataString = "";
			
			System.out.println();
			System.out.println("Please enter a command (load/search/exit)");
			
			commandString = ui.getString().toUpperCase();
					
			if(commandString.equals("LOAD")){
				System.out.println("Please enter a filename to load:");
				dataString = ui.getString();
				
			    mp.setFID(dataString);
				
				mp.splitFile(dataString);
				
			}
			
			if(commandString.equals("SEARCH")){
				System.out.println("Please enter a string to search for:");
				//if(mp.validateFile(dataString)){
					dataString = ui.getString();
				//}
				
				mp.setSID(dataString);	
				mp.sendSearch(dataString);
				
			}
			
			if(commandString.equals("EXIT")){
				mp.quit();
			}
				
		}
	}
	
}
